/**
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.ccloud.common.im.push;

import cn.hutool.core.collection.CollectionUtil;
import com.ccloud.common.core.utils.StringUtils;
import com.ccloud.common.im.enums.SocketHandlerEnum;
import com.ccloud.common.im.enums.SocketThemeEnum;
import com.ccloud.common.im.model.CIMMsg;
import com.ccloud.common.im.model.CIMSession;
import com.ccloud.common.im.respo.SessionRepository;
import com.ccloud.common.im.service.CIMSessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认消息发送实现类
 *
 */
@Slf4j
@Component
public class DefaultMessagePusher implements CIMMessagePusher {


    @Resource
    private SessionRepository sessionRepository;

    @Resource
    private CIMSessionService cimSessionService;

    @Override
    public void push(CIMMsg cimMsg) {

        ConcurrentHashMap<String, ConcurrentHashMap<String, CIMSession>> session_all = sessionRepository.getSessionMap();
        if (!CollectionUtil.isEmpty(session_all)) {
            for (ConcurrentHashMap<String, CIMSession> session_map : session_all.values()) {
                for (CIMSession session : session_map.values()) {
                    cimSessionService.push(cimMsg, session);
                }
            }
        } else {
            log.info("[server]当前没有活跃连接");
        }

    }

    @Override
    public void push(Object data, SocketHandlerEnum socketHandlerEnum, SocketThemeEnum socketThemeEnum) {
        CIMMsg cimMsg = new CIMMsg(socketHandlerEnum.getCode(), socketThemeEnum.getCode(), data);
        push(cimMsg);
    }


    @Override
    public void broadCoast(CIMMsg cimMsg, String nid) {
        ConcurrentHashMap<String, ConcurrentHashMap<String, CIMSession>> session_all = sessionRepository.getSessionMap();
        if (!CollectionUtil.isEmpty(session_all)) {
            for (ConcurrentHashMap<String, CIMSession> session_map : session_all.values()) {
                for (CIMSession session : session_map.values()) {
                    if (StringUtils.isBlank(nid)) {
                        cimSessionService.push(cimMsg, session);
                    }
                    if (StringUtils.isNotBlank(nid) && !session.getNid().equals(nid)) {
                        cimSessionService.push(cimMsg, session);
                    }
                }
            }
        } else {
            log.info("[server]当前没有活跃连接");
        }
    }


}
